//----------------------------------------------------------------------- 
// PDS WITSMLstudio Store, 2018.3
//
// Copyright 2018 PDS Americas LLC
// 
// Licensed under the PDS Open Source WITSML Product License Agreement (the
// "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//   
//     http://www.pds.group/WITSMLstudio/OpenSource/ProductLicenseAgreement
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//-----------------------------------------------------------------------

// ----------------------------------------------------------------------
// <auto-generated>
//     Changes to this file may cause incorrect behavior and will be lost
//     if the code is regenerated.
// </auto-generated>
// ----------------------------------------------------------------------

using Energistics.DataAccess;
using Energistics.DataAccess.WITSML141;
using Energistics.DataAccess.WITSML141.ComponentSchemas;
using Energistics.DataAccess.WITSML141.ReferenceData;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;

namespace PDS.WITSMLstudio.Store.Data.DepthRegImages
{
    [TestClass]
    public partial class DepthRegImage141StoreTests : DepthRegImage141TestBase
    {
        public DepthRegImage141StoreTests()
            : base(false)
        {
        }

        [TestMethod]
        public void DepthRegImage141DataAdapter_GetFromStore_Can_Get_DepthRegImage()
        {
            AddParents();
            DevKit.AddAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);
            DevKit.GetAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);
       }

        [TestMethod]
        public void DepthRegImage141DataAdapter_AddToStore_Can_Add_DepthRegImage()
        {
            AddParents();
            DevKit.AddAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);
        }

        [TestMethod]
        public void DepthRegImage141DataAdapter_UpdateInStore_Can_Update_DepthRegImage()
        {
            AddParents();
            DevKit.AddAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);
            DevKit.UpdateAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);
            DevKit.GetAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);
        }

        [TestMethod]
        public void DepthRegImage141DataAdapter_DeleteFromStore_Can_Delete_DepthRegImage()
        {
            AddParents();
            DevKit.AddAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);
            DevKit.DeleteAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);
            DevKit.GetAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage, isNotNull: false);
        }

        [TestMethod]
        public void DepthRegImage141DataAdapter_AddToStore_Creates_ChangeLog()
        {
            AddParents();

            DevKit.AddAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);

            var result = DevKit.GetAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);
            var expectedHistoryCount = 1;
            var expectedChangeType = ChangeInfoType.add;
            DevKit.AssertChangeLog(result, expectedHistoryCount, expectedChangeType);
        }

        [TestMethod]
        public void DepthRegImage141DataAdapter_UpdateInStore_Updates_ChangeLog()
        {
            AddParents();

            DevKit.AddAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);

            // Update the DepthRegImage141
            DepthRegImage.Name = "Change";
            DevKit.UpdateAndAssert(DepthRegImage);

            var result = DevKit.GetAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);
            var expectedHistoryCount = 2;
            var expectedChangeType = ChangeInfoType.update;
            DevKit.AssertChangeLog(result, expectedHistoryCount, expectedChangeType);
        }

        [TestMethod]
        public void DepthRegImage141DataAdapter_DeleteFromStore_Updates_ChangeLog()
        {
            AddParents();

            DevKit.AddAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);

            // Delete the DepthRegImage141
            DevKit.DeleteAndAssert(DepthRegImage);

            var expectedHistoryCount = 2;
            var expectedChangeType = ChangeInfoType.delete;
            DevKit.AssertChangeLog(DepthRegImage, expectedHistoryCount, expectedChangeType);
        }

        [TestMethod]
        public void DepthRegImage141DataAdapter_ChangeLog_Tracks_ChangeHistory_For_Add_Update_Delete()
        {
            AddParents();

            // Add the DepthRegImage141
            DevKit.AddAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);

            // Verify ChangeLog for Add
            var result = DevKit.GetAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);
            var expectedHistoryCount = 1;
            var expectedChangeType = ChangeInfoType.add;
            DevKit.AssertChangeLog(result, expectedHistoryCount, expectedChangeType);

            // Update the DepthRegImage141
            DepthRegImage.Name = "Change";
            DevKit.UpdateAndAssert(DepthRegImage);

            result = DevKit.GetAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);
            expectedHistoryCount = 2;
            expectedChangeType = ChangeInfoType.update;
            DevKit.AssertChangeLog(result, expectedHistoryCount, expectedChangeType);

            // Delete the DepthRegImage141
            DevKit.DeleteAndAssert(DepthRegImage);

            expectedHistoryCount = 3;
            expectedChangeType = ChangeInfoType.delete;
            DevKit.AssertChangeLog(DepthRegImage, expectedHistoryCount, expectedChangeType);

            // Re-add the same DepthRegImage141...
            DevKit.AddAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);

            //... the same changeLog should be reused.
            result = DevKit.GetAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);
            expectedHistoryCount = 4;
            expectedChangeType = ChangeInfoType.add;
            DevKit.AssertChangeLog(result, expectedHistoryCount, expectedChangeType);

            DevKit.AssertChangeHistoryTimesUnique(result);
        }

        [TestMethod]
        public void DepthRegImage141DataAdapter_GetFromStore_Filter_ExtensionNameValue()
        {
            AddParents();

            var extensionName1 = DevKit.ExtensionNameValue("Ext-1", "1.0", "m");
            var extensionName2 = DevKit.ExtensionNameValue("Ext-2", "2.0", "cm", PrimitiveType.@float);
            extensionName2.MeasureClass = MeasureClass.Length;
            var extensionName3 = DevKit.ExtensionNameValue("Ext-3", "3.0", "cm", PrimitiveType.unknown);

            DepthRegImage.CommonData = new CommonData()
            {
                ExtensionNameValue = new List<ExtensionNameValue>()
                {
                    extensionName1, extensionName2, extensionName3
                }
            };

            // Add the DepthRegImage141
            DevKit.AddAndAssert(DepthRegImage);

            // Query for first extension
            var commonDataXml = "<commonData>" + Environment.NewLine +
                                "<extensionNameValue uid=\"\">" + Environment.NewLine +
                                "<name />{0}" + Environment.NewLine +
                                "</extensionNameValue>" + Environment.NewLine +
                                "</commonData>";

            var extValueQuery = string.Format(commonDataXml, "<dataType>double</dataType>");
            var queryXml = string.Format(BasicXMLTemplate, DepthRegImage.UidWell, DepthRegImage.UidWellbore, DepthRegImage.Uid, extValueQuery);
            var result = DevKit.Query<DepthRegImageList, DepthRegImage>(ObjectTypes.DepthRegImage, queryXml, null, OptionsIn.ReturnElements.Requested);
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);

            var resultDepthRegImage = result[0];
            Assert.IsNotNull(resultDepthRegImage);

            var commonData = resultDepthRegImage.CommonData;
            Assert.IsNotNull(commonData);
            Assert.AreEqual(1, commonData.ExtensionNameValue.Count);

            var env = commonData.ExtensionNameValue[0];
            Assert.IsNotNull(env);
            Assert.AreEqual(extensionName1.Uid, env.Uid);
            Assert.AreEqual(extensionName1.Name, env.Name);

            // Query for second extension
            extValueQuery = string.Format(commonDataXml, "<measureClass>length</measureClass>");
            queryXml = string.Format(BasicXMLTemplate, DepthRegImage.UidWell, DepthRegImage.UidWellbore, DepthRegImage.Uid, extValueQuery);
            result = DevKit.Query<DepthRegImageList, DepthRegImage>(ObjectTypes.DepthRegImage, queryXml, null, OptionsIn.ReturnElements.Requested);
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);

            resultDepthRegImage = result[0];
            Assert.IsNotNull(resultDepthRegImage);

            commonData = resultDepthRegImage.CommonData;
            Assert.IsNotNull(commonData);
            Assert.AreEqual(1, commonData.ExtensionNameValue.Count);

            env = commonData.ExtensionNameValue[0];
            Assert.IsNotNull(env);
            Assert.AreEqual(extensionName2.Uid, env.Uid);
            Assert.AreEqual(extensionName2.Name, env.Name);

            // Query for third extension
            extValueQuery = string.Format(commonDataXml, "<dataType>unknown</dataType>");
            queryXml = string.Format(BasicXMLTemplate, DepthRegImage.UidWell, DepthRegImage.UidWellbore, DepthRegImage.Uid, extValueQuery);
            result = DevKit.Query<DepthRegImageList, DepthRegImage>(ObjectTypes.DepthRegImage, queryXml, null, OptionsIn.ReturnElements.Requested);
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);

            resultDepthRegImage = result[0];
            Assert.IsNotNull(resultDepthRegImage);

            commonData = resultDepthRegImage.CommonData;
            Assert.IsNotNull(commonData);
            Assert.AreEqual(1, commonData.ExtensionNameValue.Count);

            env = commonData.ExtensionNameValue[0];
            Assert.IsNotNull(env);
            Assert.AreEqual(extensionName3.Uid, env.Uid);
            Assert.AreEqual(extensionName3.Name, env.Name);
        }

        [TestMethod]
        public void DepthRegImage141DataAdapter_ChangeLog_Syncs_DepthRegImage_Name_Changes()
        {
            AddParents();

            // Add the DepthRegImage141
            DevKit.AddAndAssert<DepthRegImageList, DepthRegImage>(DepthRegImage);

            // Assert that all DepthRegImage names match corresponding changeLog names
            DevKit.AssertChangeLogNames(DepthRegImage);

            // Update the DepthRegImage141 names
            DepthRegImage.Name = "Change";
            DepthRegImage.NameWell = "Well Name Change";

            DepthRegImage.NameWellbore = "Wellbore Name Change";

            DevKit.UpdateAndAssert(DepthRegImage);

            // Assert that all DepthRegImage names match corresponding changeLog names
            DevKit.AssertChangeLogNames(DepthRegImage);
        }
    }
}